﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;

public partial class UserControls_SelectInvolvedStep : System.Web.UI.UserControl
{
    private static readonly ILog log = LogManager.GetLogger("LogFile");

    #region Propiedades

    public int TramiteId
    {
        get
        {
            int procesoId = 0;
            try
            {
                procesoId = Convert.ToInt32(TramiteIdHiddenField.Value);
            }
            catch (Exception ex)
            {
                log.Error("No se pudo convertir TramiteIdHiddenField.Value a un valor entero", ex);
            }
            return procesoId;
        }
        set { TramiteIdHiddenField.Value = value.ToString(); }
    }

    public bool ReadOnly
    {
        set { ReadOnlyHiddenField.Value = value.ToString(); }
        get 
        {
            bool readOnly = false;
            try
            {
                readOnly = Convert.ToBoolean(ReadOnlyHiddenField.Value);
            }
            catch (Exception ex)
            {
                log.Error("No se pudo convertir ReadOnlyHiddenField.Value a un valor boolean", ex);
            }
            return readOnly;
        }
    }

    protected string SearchRequestHandler
    {
        get { return VirtualPathUtility.ToAbsolute("~/UserControls/Handlers/SearchPeopleForInvolved.ashx"); }
    }

    public string InvolucradosInsertados
    {
        get { return InsertedInvolvedHiddenField.Value; }
        set { InsertedInvolvedHiddenField.Value = value; }
    }

    public string InvolucradosEliminados
    {
        get { return DeletedInvolvedHiddenField.Value; }
        set { DeletedInvolvedHiddenField.Value = value; }
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Update();
    }

    protected string GetClassName()
    {
        return ClientID + "_removePerson";
    }

    protected bool IsRepeaterVisible()
    {
        return InvolvedRepeater.Items.Count == 0;
    }

    public string GetValueHtml()
    {
        return "<input type='hidden' value='' name='" + UniqueID + "' id='" + ClientID + "'/>";
    }

    public string GetControlValue()
    {
        return InvolvedRepeater.Items.Count == 0 ? "" : "1";
    }

    public void Update()
    {
        List<Iudicium.Servicio.Involucrado> involvedList = new List<Iudicium.Servicio.Involucrado>();
        if (TramiteId > 0)
        {
            try
            {
                involvedList =
                    Iudicium.Servicios.CRN.InvolucradoCRN.GetInvolucradosByServicioId(TramiteId, Iudicium.Servicio.Involucrado.TipoInvolucrado.Participante);
            }
            catch (Exception ex)
            {
                log.Error("No se pudo cargar la lista de demandados para el proceso " + TramiteId, ex);
            }

        }
        if (ReadOnly)
        {
            SelectInvolvedHyperLink.Visible = false;
        }
        InvolvedRepeater.DataSource = involvedList;
        InvolvedRepeater.DataBind();
    }
}